package com.lee.dutylower.db.greendao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.lee.dutylower.db.greenEntity.SoldierSentry;

import com.lee.dutylower.db.greenEntity.Sentry;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "SENTRY".
*/
public class SentryDao extends AbstractDao<Sentry, Long> {

    public static final String TABLENAME = "SENTRY";

    /**
     * Properties of entity Sentry.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Sentry_id = new Property(0, Long.class, "sentry_id", true, "_id");
        public final static Property Sentry_name = new Property(1, String.class, "sentry_name", false, "SENTRY_NAME");
        public final static Property Sentry_type = new Property(2, Integer.class, "sentry_type", false, "SENTRY_TYPE");
    }

    private DaoSession daoSession;

    private Query<Sentry> soldier_SentryGroupQuery;

    public SentryDao(DaoConfig config) {
        super(config);
    }
    
    public SentryDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"SENTRY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: sentry_id
                "\"SENTRY_NAME\" TEXT," + // 1: sentry_name
                "\"SENTRY_TYPE\" INTEGER);"); // 2: sentry_type
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"SENTRY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Sentry entity) {
        stmt.clearBindings();
 
        Long sentry_id = entity.getSentry_id();
        if (sentry_id != null) {
            stmt.bindLong(1, sentry_id);
        }
 
        String sentry_name = entity.getSentry_name();
        if (sentry_name != null) {
            stmt.bindString(2, sentry_name);
        }
 
        Integer sentry_type = entity.getSentry_type();
        if (sentry_type != null) {
            stmt.bindLong(3, sentry_type);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Sentry entity) {
        stmt.clearBindings();
 
        Long sentry_id = entity.getSentry_id();
        if (sentry_id != null) {
            stmt.bindLong(1, sentry_id);
        }
 
        String sentry_name = entity.getSentry_name();
        if (sentry_name != null) {
            stmt.bindString(2, sentry_name);
        }
 
        Integer sentry_type = entity.getSentry_type();
        if (sentry_type != null) {
            stmt.bindLong(3, sentry_type);
        }
    }

    @Override
    protected final void attachEntity(Sentry entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Sentry readEntity(Cursor cursor, int offset) {
        Sentry entity = new Sentry( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // sentry_id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // sentry_name
            cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2) // sentry_type
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Sentry entity, int offset) {
        entity.setSentry_id(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setSentry_name(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setSentry_type(cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Sentry entity, long rowId) {
        entity.setSentry_id(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Sentry entity) {
        if(entity != null) {
            return entity.getSentry_id();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Sentry entity) {
        return entity.getSentry_id() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "sentryGroup" to-many relationship of Soldier. */
    public List<Sentry> _querySoldier_SentryGroup(Long soldierId) {
        synchronized (this) {
            if (soldier_SentryGroupQuery == null) {
                QueryBuilder<Sentry> queryBuilder = queryBuilder();
                queryBuilder.join(SoldierSentry.class, SoldierSentryDao.Properties.SentryId)
                    .where(SoldierSentryDao.Properties.SoldierId.eq(soldierId));
                soldier_SentryGroupQuery = queryBuilder.build();
            }
        }
        Query<Sentry> query = soldier_SentryGroupQuery.forCurrentThread();
        query.setParameter(0, soldierId);
        return query.list();
    }

}
